﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using Services;

using System.IO.IsolatedStorage;
using System.IO;
using Utilities;
//using BowMaster.Menu;

namespace SuperTicTacToe
{

  public class GameState
  {
      public int Current_level { get; set; }
      public ArrayList<int> Records { get; set; }
      public ArrayList<bool> Available { get; set; }
      //public GameState(int current_level, ArrayList<float> records)
      //{
      //    this.Current_level = current_level;
      //    this.Records = records;
      //}
      public GameState()
      {
          Current_level = 0;
          Records = new ArrayList<int>();
          Available = new ArrayList<bool>();
          //for (int i = 0; i < StateManager.NUMBER_OF_LEVELS; i++)
          //{
          //    Records.Add(int.MaxValue);
          //    Available.Add(false);
          //}
      }
  }
  //public class OptionsState
  //{
  //    public bool sound;
  //    //public InputType inputType;
  //    public OptionsState()
  //    {
  //        sound = true;
  //        inputType = 0;
  //    }
  //    public OptionsState(bool sound, InputType type)
  //    {
  //        this.sound = sound;
  //        this.inputType = type;
  //    }
  //}
  public static class Storage
  {

    

    private static Func<StreamReader, GameState> read_game_state = reader =>
    {
        GameState gs = new GameState();

        var s = reader.ReadToEnd();
        var r = s.Split(new[] { ';' });

        if (r.Length != 2)
            return gs;
        gs.Current_level = int.Parse(r[0]);
        gs.Records.Clear();
        gs.Available.Clear();
        r = r[1].Split(new[] { '|' });
        //for (int i = 0; i < StateManager.NUMBER_OF_LEVELS; i++)
        //{
        //    if (i < r.Length)
        //    {
        //        var l = r[i].Split(new[] { '/' });
        //        gs.Records.Add(int.Parse(l[1]));
        //        gs.Available.Add(int.Parse(l[0]) == 1 ? true : false);
        //    }
        //    else
        //    {
        //        gs.Records.Add(int.MaxValue);
        //        gs.Available.Add(false);
        //    }
        //}
        return gs;
    };

    private static Action<StreamWriter, GameState> write_game_state = (writer, entry) =>
        {
            var s = "" + entry.Current_level + ";\n";
            for (int i = 0; i < entry.Records.Count; i++)
            {
                if (i < entry.Records.Count - 1)
                    s += (entry.Available[i] ? 1 : 0) + "/" + entry.Records[i] + "|";
                else
                    s += (entry.Available[i] ? 1 : 0) + "/" + entry.Records[i];
                s += "\n";
            }
            writer.Write(s);
        };


    public static void SaveGameState(GameState entry)
    {
#if WINDOWS || XBOX
        using (var writer = new StreamWriter("campaign.txt"))
            write_game_state(writer, entry);
#else
      using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
      {
        appStorage.DeleteFile("campaign.txt");
        using (var streamFile = appStorage.OpenFile("campaign.txt", FileMode.OpenOrCreate))
        using (var writer = new StreamWriter(streamFile))
            write_game_state(writer, entry);
      }
#endif
    }

    

    public static GameState ResetGameState()
    {
        var gs = new GameState();
        SaveGameState(gs);
        return gs;
    }

    public static GameState GetGameState()
    {
#if WINDOWS || XBOX
        try
        {
        using (var reader = new StreamReader("campaign.txt"))
          return read_game_state(reader);
      }
      catch (Exception)
        { return ResetGameState(); }
#else
      using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
      {
        using (var streamFile = appStorage.OpenFile("campaign.txt", FileMode.OpenOrCreate))
        {
          try
          {
            using (var reader = new StreamReader(streamFile))
                return read_game_state(reader);
          }
          catch (Exception) { return ResetGameState(); }
        }
      }
#endif
    }


//    private static Func<StreamReader, OptionsState> read_options_state = reader =>
//    {
//        OptionsState os = new OptionsState();

//        var s = reader.ReadToEnd();
//        var r = s.Split(new[] { ';' });

//        if (r.Length != 2)
//            return os;
//        os.sound = bool.Parse(r[0]);
//        os.inputType = (InputType)(int.Parse(r[1]));
//        return os;
//    };

//    private static Action<StreamWriter, OptionsState> write_options_state = (writer, entry) =>
//        {
//            var s = "" + entry.sound + ";" + (int)(entry.inputType);
//            writer.Write(s);
//        };


//    public static void SaveOptionsState(OptionsState entry)
//    {
//#if WINDOWS || XBOX
//        using (var writer = new StreamWriter("options.txt"))
//            write_options_state(writer, entry);
//#else
//      using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
//      {
//        appStorage.DeleteFile("options.txt");
//        using (var streamFile = appStorage.OpenFile("options.txt", FileMode.OpenOrCreate))
//        using (var writer = new StreamWriter(streamFile))
//            write_options_state(writer, entry);
//      }
//#endif
//    }



//    public static OptionsState ResetOptionsState()
//    {
//        var os = new OptionsState();
//        SaveOptionsState(os);
//        return os;
//    }

//    public static OptionsState GetOptionsState()
//    {
//#if WINDOWS || XBOX
//        try
//        {
//            using (var reader = new StreamReader("options.txt"))
//                return read_options_state(reader);
//        }
//        catch (Exception)
//        { return ResetOptionsState(); }
//#else
//      using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
//      {
//        using (var streamFile = appStorage.OpenFile("options.txt", FileMode.OpenOrCreate))
//        {
//          try
//          {
//            using (var reader = new StreamReader(streamFile))
//                return read_options_state(reader);
//          }
//          catch (Exception) { return ResetOptionsState(); }
//        }
//      }
//#endif
//    }

  }
}
